﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DBLibrary;
using Common;

public partial class LoginPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void LoginImageButton_Click(object sender, ImageClickEventArgs e)
    {
        string adminName = NameTextBox.Text;
        string pwd = PwdTextBox.Text;
        DB db = PoseidonDBHelper.AdminDB;
        List<AdminDBDetail> destList = db.Select<AdminDBDetail>(new DBParam("AdminName", adminName), new DBParam("Pwd", pwd));
        if (destList != null && destList.Count > 0)
        {
            AdminDBDetail admin = destList[0];
            if (adminName == "backStageAdmin")
            {
                FormsAuthentication.SetAuthCookie(adminName, false);
                this.Session.SetUserDBPrefix(adminName, admin.DBPre);
                this.Response.Redirect("~/backstage/BackConsolePage.aspx");
            }
            else
            {
                List<KeyDetail> keyList = db.Select<KeyDetail>(new DBParam("DBPre", admin.DBPre));
                if (keyList != null && keyList.Count > 0)
                {
                    KeyContent kc = keyList[0].KeyContent;
                    if (kc.StartTime > kc.ExpireTime)
                    {
                        ErrorLabel.Text = "您的使用期限已到";
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(adminName, false);
                        this.Session.SetUserDBPrefix(adminName, admin.DBPre);
                        this.Response.Redirect("~/masterPages/main/MainDefault.aspx");
                    }
                }
                else
                {
                    ErrorLabel.Text = "您的使用期限已到";
                }
            }
        }
        else
        { 
            ErrorLabel.Text = "用户名或密码不正确";
        }
    }
    
    protected void AddQuickButton_Click(object sender, EventArgs e)
    {
        string name = QName.Text;
        string pwd = QP1.Text;
        if (PoseidonDBHelper.AdminDB.Exist<AdminDBDetail>(new DBParam("AdminName", name)))
        {
            ErrorLabel.Text = "用户名重复";
            return;
        }
        KeyHelper.RetrieveTemporaryKeys();
        KeyDetail kd = KeyHelper.GetFreeKey(KeyUsageType.Temporary);
        if (kd == null)
        {
            ErrorLabel.Text = "试用数量已达上限，请过几天再试";
            return;
        }

        AdminDBDetail ad = new AdminDBDetail();
        ad.AdminName = name;
        ad.PassWord = pwd;
        PoseidonDBHelper.AdminDB.Insert(ad);

        kd.IsInUse = true;
        kd.KeyContent = KeyContent.GenerateKey();
        kd.DBPre = ad.DBPre;
        PoseidonDBHelper.AdminDB.Update(kd, "KeyId");

        this.Session.SetUserDBPrefix(name, ad.DBPre);
        PoseidonDBHelper.CreateNewUserDB(ad.DBPre);
        FormsAuthentication.SetAuthCookie(name, false);
        this.Response.Redirect("~/masterPages/main/MainDefault.aspx");
    }
}